package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.repository.ICategoryRepository;
import cn.tedu.flower.manage.content.dao.persist.repository.IGoodsRepository;
import cn.tedu.flower.manage.content.pojo.entity.Goods;
import cn.tedu.flower.manage.content.pojo.param.GoodsAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.GoodsUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.CategoryStandardVO;
import cn.tedu.flower.manage.content.pojo.vo.GoodsListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.GoodsStandardVO;
import cn.tedu.flower.manage.content.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Value("${flower-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IGoodsRepository goodsRepository;
    @Autowired
    private ICategoryRepository categoryRepository;


    @Override
    public void addNew(CurrentPrincipal currentPrincipal,  GoodsAddNewParam goodsAddNewParam) {
        log.debug("开始处理【发布商品】的业务，当事人：{}，参数：{}", currentPrincipal,goodsAddNewParam);
        CategoryStandardVO categoryStandardVO = categoryRepository.getStandardById(goodsAddNewParam.getCategoryId());
        if (categoryStandardVO == null) {
            String message="添加商品失败,该类别不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        if (categoryStandardVO.getEnable()==0) {
            String message="添加商品失败,该类别未启用";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        Goods goods=new Goods();
        BeanUtils.copyProperties(goodsAddNewParam,goods);
        int rows = goodsRepository.insert(goods);
        if (rows!=1) {
            String message="添加商品失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除商品】的业务，参数：{}", id);
        GoodsStandardVO standardById = goodsRepository.getStandardById(id);
        if (standardById==null) {
            String message="删除商品失败,尝试删除的商品数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        int rows = goodsRepository.deleteById(id);
        if (rows!=1) {
            String message = "删除商品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(GoodsUpdateParam goodsUpdateParam) {
        log.debug("开始处理【修改商品】的业务,参数:{}",goodsUpdateParam);
        GoodsStandardVO standardById = goodsRepository.getStandardById(goodsUpdateParam.getId());
        if (standardById==null) {
            String message="修改商品信息失败,该商品数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        Goods goods=new Goods();
        BeanUtils.copyProperties(goodsUpdateParam,goods);
        goods.setGoodsStatus(standardById.getGoodsStatus());
        goods.setId(goodsUpdateParam.getId());
        int rows = goodsRepository.update(goods);
        if (rows!=1) {
            String message="修改商品信息失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }


    }

    @Override
    public void putOn(Long id) {
        log.debug("开始处理【上架商品】的业务，参数：{}", id);
        updateStatusById(id,1);
    }

    @Override
    public void pullOff(Long id) {
        log.debug("开始处理【下架商品】的业务，参数：{}", id);
        updateStatusById(id,0);
    }

    @Override
    public void reduceNumber() {

    }

    @Override
    public GoodsStandardVO getStandardById(Long id) {

        GoodsStandardVO standardById = goodsRepository.getStandardById(id);
        if (standardById==null) {
            String message="查询商品详情失败,该商品不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standardById;
    }

    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询商品列表】的业务，页码：{}", pageNum);
        PageData<GoodsListItemVO> pageData = goodsRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询商品列表】的业务，页码：{}", pageNum);
        PageData<GoodsListItemVO> pageData = goodsRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> listByCategoryId(Long categoryId, Integer pageNum) {
        log.debug("开始处理【根据类别查询商品列表】的业务，分类ID:{},页码：{}",categoryId, pageNum);
        PageData<GoodsListItemVO> pageData = goodsRepository.listByCategoryId(categoryId, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> listByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据类别查询商品列表】的业务，分类ID:{},页码：{}",categoryId, pageNum);
        PageData<GoodsListItemVO> pageData = goodsRepository.listByCategoryId(categoryId, pageNum, pageSize);
        return pageData;
    }

    private void updateStatusById(Long id,Integer status){
        GoodsStandardVO goodsStandardVO = goodsRepository.getStandardById(id);
        if (goodsStandardVO==null) {
            String message="将商品状态修改为"+STATUS_TEXT[status]+"失败,商品数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        if (goodsStandardVO.getGoodsStatus()==status) {
            String message="将商品状态修改为"+STATUS_TEXT[status]+"失败,该商品已"+STATUS_TEXT[status];
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        Goods updateGoods=new Goods();
        updateGoods.setId(id);
        updateGoods.setGoodsStatus(status);
        int rows = goodsRepository.update(updateGoods);
        if (rows!=1) {
            String message="将商品状态修改为"+STATUS_TEXT[status]+"失败,服务器繁忙,请稍后后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }

    }

}
